import sun.awt.image.ImageWatched;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Microsoft
 * Date: 2023-03-04
 * Time: 10:32
 */
public class Test {
    //判断出栈顺序是否合法
    public boolean isPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
            while (j < popA.length && !stack.empty() && stack.peek().equals(popA[j])) {//这里有一个隐蔽的问题就是Integer在-128到127才能用==进行判断是否相等，所以最好使用equals
                //判断stack是否为空和j下标的范围一定要在equals方法之前
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }


    //判断括号是否匹配
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '[' ||ch == '{' ) {
                stack.push(ch);
            }else {
                //遇到了右括号
                if (stack.empty()) {
                    return false;//第一个不能是右括号
                }
                char ch2 = stack.peek();//获取左括号
                if(ch2 == '(' && ch == ')' ||ch2 == '{' && ch == '}' ||ch2 == '[' && ch == ']' ){
                    stack.pop();
                }else {
                    return false;//不匹配
                }
            }
        }
        if (!stack.empty()){
            return false;
        }
        return true;
    }


    public int evalRPN(String[] tokens) {
        //tokens是一个后缀表达式
        Stack<Integer> stack = new Stack<>();
        for (String x:tokens) {
            if (!isOperation(x)){
                stack.push(Integer.parseInt(x));
            }else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (x) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private boolean isOperation(String x){
        //判断是否是操作符
        if (x.equals("+")||x.equals("-")||x.equals("*")||x.equals("/")) {
            return true;
        }
        return false;
    }
}


//public static void main1(String[] args) {
//        LinkedList<String> linkedList1 = new LinkedList<>();
//        linkedList1.add("hello");
//        linkedList1.add("abc");
//        linkedList1.add("def");
//        linkedList1.add("ok");
//
//        //通过sout直接进行打印
//        System.out.println(linkedList1);
//        //通过for-each
//        for (String x:linkedList1) {
//            System.out.print(x + " ");
//        }
//        //通过迭代器 正向打印
//        System.out.println();
//        ListIterator<String> it = linkedList1.listIterator();
//        while (it.hasNext()) {
//            System.out.print(it.next() + " ");//如果还有下一个元素，那么就将下一个元素进行打印，同时it向后走一步
//
//        }
//        //通过迭代器 反向打印
//        System.out.println();
//        ListIterator<String> it2 = linkedList1.listIterator(linkedList1.size());
//        while (it2.hasPrevious()) {
//            System.out.print(it2.previous() + " ");
//
//        }
//    }
